package com.example.itshome.activity;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.*;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;
import com.bumptech.glide.Glide;
import com.example.itshome.R;
import com.example.itshome.adapter.CircleImageAdapter;
import com.example.itshome.adapter.CommonRecycleViewAdapter;
import com.example.itshome.entity.CircleEntity;
import com.example.itshome.entity.CommonEntity;
import com.example.itshome.entity.MyInteger;
import com.example.itshome.network.AbstractNetworkRequest;
import com.example.itshome.network.GetCommonRequest;
import com.example.itshome.network.GiveLikeRequest;
import com.example.itshome.network.SendCommonRequest;

import java.util.ArrayList;
import java.util.List;

public class CircleParticularActivity extends AppCompatActivity {

    private ImageView backImageView;
    private ImageView senderHeaderImageView;
    private ImageView likeImageView;
    private TextView senderName;
    private TextView descriptionTextView;
    private TextView likeCountTextView;
    private TextView timeTextView;
    private TextView commonCountTextView;
    private ScrollView scrollView;
    private EditText commonEditText;
    private View likeLayout;
    private LinearLayout vpBottomLayout;
    private ViewPager imageViewPager;
    private RecyclerView mRecyclerView;
    private List<CommonEntity> mRecyclerViewData;
    private final List<String> images=new ArrayList<>();
    private int currentImageIndex;
    private CircleEntity entity;
    private final MyInteger commonPageIndex=new MyInteger();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_circle_particular);
        initData();
        initFind();
        initView();
        initButton();
        initRecycleView();
        initScrollView();
    }

    void initData(){
        Intent intent=getIntent();
        entity=new CircleEntity();
        entity.setId(intent.getIntExtra("circleId",-1))
                .setLiked(intent.getBooleanExtra("isLike",false));
        String[] data=intent.getStringArrayExtra("data");
        entity.setTitle(data[0])
                .setSenderName(data[1])
                .setSenderHeaderPictureSrc(data[2])
                .setSendTime(data[3])
                .setLikeCount(data[4]);
    }

    void initFind(){
        scrollView=findViewById(R.id.sv_circle_particular_page);
        commonCountTextView=findViewById(R.id.txv_circle_particular_page_comment_count);
        backImageView=findViewById(R.id.imv_circle_particular_page_back);
        senderHeaderImageView=findViewById(R.id.imv_circle_particular_page_sender_header);
        likeImageView=findViewById(R.id.imv_circle_particular_page_like);
        senderName=findViewById(R.id.txv_circle_particular_page_sender_name);
        descriptionTextView=findViewById(R.id.txv_circle_particular_page_description);
        likeCountTextView=findViewById(R.id.txv_circle_particular_page_like_count);
        commonEditText=findViewById(R.id.edt_circle_particular_page_common);
        likeLayout=findViewById(R.id.layout_circle_particular_page_like);
        vpBottomLayout=findViewById(R.id.layout_circle_particular_page_vp_bottom);
        imageViewPager=findViewById(R.id.vp_circle_particular_page);
        mRecyclerView=findViewById(R.id.rcv_circle_particular_page);
        timeTextView=findViewById(R.id.txv_circle_particular_page_time);
    }

    void initButton(){
        backImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        likeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AbstractNetworkRequest request=new GiveLikeRequest(CircleParticularActivity.this)
                        .setEntity(entity)
                        .setLikeImageView(likeImageView)
                        .setLikeCountTextView(likeCountTextView);
                request.start();
            }
        });
    }

    void initScrollView(){
        scrollView.setOnScrollChangeListener(new View.OnScrollChangeListener() {

            private boolean isCan=true;
            @Override
            public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                View vv=scrollView.getChildAt(0);
                int childHeight = vv.getHeight();//获取子控件高度
                int height = scrollView.getHeight();//获取Scrollview的控件高度
                if(scrollY+height == childHeight){//判断条件 当子控件高度=Scrollview的控件高度+x的时候控件到达底部
                    if(isCan){
                        isCan=false;
                        AbstractNetworkRequest request=new GetCommonRequest(CircleParticularActivity.this)
                                .setCircleId(entity.getId())
                                .setCommonPageIndex(commonPageIndex)
                                .setCommonCountTextView(commonCountTextView)
                                .setFinishRunnable(new Runnable() {
                                    @Override
                                    public void run() {
                                        isCan=true;
                                    }
                                })
                                .setAdapter(mRecyclerView.getAdapter())
                                .setCurrentData(mRecyclerViewData);
                        request.start();
                    }

                }
            }



        });
    }

    void initViewPager(){
        imageViewPager.setAdapter(new CircleImageAdapter(images,getLayoutInflater(),this));
        imageViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                vpBottomLayout.getChildAt(currentImageIndex).setEnabled(false);
                vpBottomLayout.getChildAt(position).setEnabled(true);
                currentImageIndex=position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        LinearLayout.LayoutParams paramsFirstItem=new LinearLayout.LayoutParams(20,20);
        LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(20,20);
        params.setMarginStart(30);
        int count= images.size();
        for(int i=0;i<count;i++){
            View view=new View(this);
            view.setBackgroundResource(R.drawable.slt_circle_particular_page_vp_bottom_item);
            view.setEnabled(false);
            vpBottomLayout.addView(view,i==0?paramsFirstItem:params);
        }
        if(count>0){
            vpBottomLayout.getChildAt(0).setEnabled(true);
            currentImageIndex=0;
        }
    }

    void initRecycleView(){
        mRecyclerViewData=new ArrayList<>();
        mRecyclerView.setAdapter(new CommonRecycleViewAdapter(this,mRecyclerViewData));
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setFocusable(false);
        AbstractNetworkRequest request=new GetCommonRequest(this)
                .setCircleId(entity.getId())
                .setCommonPageIndex(commonPageIndex)
                .setImages(images)
                .setCommonCountTextView(commonCountTextView)
                .setInitViewPager(new Runnable() {
                    @Override
                    public void run() {
                        initViewPager();
                    }
                })
                .setAdapter(mRecyclerView.getAdapter())
                .setCurrentData(mRecyclerViewData);
        request.start();
    }

    void initView(){
        senderName.setText(entity.getSenderName());
        descriptionTextView.setText(entity.getTitle());
        likeCountTextView.setText(entity.getLikeCount());
        timeTextView.setText(entity.getSendTime());
        Glide.with(this).load(entity.getSenderHeaderPictureSrc()).placeholder(R.drawable.my_page_header_example).circleCrop().into(senderHeaderImageView);
        likeImageView.setSelected(entity.isLiked());

        commonEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if(actionId== EditorInfo.IME_ACTION_SEND){
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(commonEditText.getWindowToken(), 0);
                    AbstractNetworkRequest request=new SendCommonRequest(CircleParticularActivity.this)
                            .setCircleId(entity.getId())
                            .setMessage(commonEditText.getText().toString())
                            .setCommonCountTextView(commonCountTextView)
                            .setAdapter((CommonRecycleViewAdapter) mRecyclerView.getAdapter())
                            .setRun(new Runnable() {
                                @Override
                                public void run() {
                                    commonEditText.setText("");
                                }
                            });
                    request.start();
                }
                return false;
            }
        });
    }
}